using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Common;
using Common.Enum;
using Common.NotifyEvent;

namespace SSettings.SoftSetting
{
	[Serializable]
	public class BasicSetting : NotifyPropertyChanged
	{
		private int _Customer;

		private bool _Compensator = true;

		private EAutoSaveType _AutoSaveOption;

		public string _FileExt = "csv";

		public List<KeyValuePair> _HotlidTemperatures;

		private bool _CustomImport;

		private string _CustomImportName;

		public int Customer
		{
			get
			{
				return _Customer;
			}
			set
			{
				_Customer = value;
			}
		}

		public bool Compensator
		{
			get
			{
				return _Compensator;
			}
			set
			{
				if (_Compensator != value)
				{
					_Compensator = value;
					OnPropertyChanged("Compensator");
				}
			}
		}

		public EAutoSaveType AutoSaveOption
		{
			get
			{
				return _AutoSaveOption;
			}
			set
			{
				if (_AutoSaveOption != value)
				{
					_AutoSaveOption = value;
					OnPropertyChanged("AutoSaveOption");
				}
			}
		}

		public string FileExt
		{
			get
			{
				return _FileExt;
			}
			set
			{
				if (!(_FileExt == value))
				{
					_FileExt = value;
					OnPropertyChanged("FileExt");
				}
			}
		}

		public List<KeyValuePair> HotlidTemperatures
		{
			get
			{
				return _HotlidTemperatures;
			}
			set
			{
				if (_HotlidTemperatures != value)
				{
					_HotlidTemperatures = value;
					OnPropertyChanged("HotlidTemperatures");
				}
			}
		}

		public bool CustomImport
		{
			get
			{
				return _CustomImport;
			}
			set
			{
				if (_CustomImport != value)
				{
					_CustomImport = value;
					OnPropertyChanged("CustomImport");
				}
			}
		}

		public string CustomImportName
		{
			get
			{
				return _CustomImportName;
			}
			set
			{
				if (!(_CustomImportName == value))
				{
					_CustomImportName = value;
					OnPropertyChanged("CustomImportName");
				}
			}
		}

		public string CustomImportFullName
		{
			get
			{
				return Path.Combine(Utility.GetApplicationPath(), CustomImportName);
			}
		}

		public BasicSetting()
		{
			GetAllTypeTemperatures();
		}

		public int GetHotlidTemperature(EProjectType Type)
		{
			int result = 105;
			foreach (KeyValuePair hotlidTemperature in HotlidTemperatures)
			{
				if ((EProjectType)hotlidTemperature.Key == Type)
				{
					result = (int)hotlidTemperature.Value;
				}
			}
			return result;
		}

		public void SetHotlidTemperature(EProjectType Type, int Temperature)
		{
			foreach (KeyValuePair hotlidTemperature in HotlidTemperatures)
			{
				if ((EProjectType)hotlidTemperature.Key == Type)
				{
					hotlidTemperature.Value = Temperature;
				}
			}
		}

		public void SaveToStream(StreamEx stream)
		{
			List<PropertyKey> list = new List<PropertyKey>();
			List<byte[]> list2 = new List<byte[]>();
			StreamEx streamEx = new StreamEx();
			list.Add(PropertyKey.Key_HotlidTemperatureList);
			streamEx.Write(HotlidTemperatures.Count);
			foreach (KeyValuePair hotlidTemperature in HotlidTemperatures)
			{
				streamEx.Write((int)(EProjectType)hotlidTemperature.Key);
				streamEx.Write((int)hotlidTemperature.Value);
			}
			list2.Add(streamEx.ToArray());
			if (CustomImport)
			{
				list.Add(PropertyKey.Key_CustomImport);
				list2.Add(BytesConverter.GetBytes(CustomImport));
				if (!string.IsNullOrEmpty(CustomImportName))
				{
					list.Add(PropertyKey.Key_CustomImportName);
					list2.Add(BytesConverter.GetBytes(CustomImportName));
				}
			}
			list.Add(PropertyKey.Key_AutoSaveOption);
			list2.Add(BytesConverter.GetBytes((int)AutoSaveOption));
			list.Add(PropertyKey.Key_FileExt);
			list2.Add(BytesConverter.GetBytes(FileExt));
			stream.Write(PropertyKey.Key_BasicSetting);
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(list, list2);
			stream.Flush();
		}

		public bool ReadFromStream(StreamEx stream)
		{
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey != PropertyKey.Key_BasicSetting)
			{
				return false;
			}
			propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_Index)
			{
				List<PropertyKey> keys;
				List<byte[]> dates;
				stream.ReadIndex(out keys, out dates);
				for (int i = 0; i < keys.Count; i++)
				{
					switch (keys[i])
					{
					case PropertyKey.Key_HotlidTemperatureList:
					{
						StreamEx streamEx = new StreamEx();
						streamEx.Write(dates[i]);
						streamEx.Seek(0L, SeekOrigin.Begin);
						int num = streamEx.ReadInt();
						for (int j = 0; j < num; j++)
						{
							KeyValuePair keyValue = new KeyValuePair();
							keyValue.Key = (EProjectType)streamEx.ReadInt();
							keyValue.Value = streamEx.ReadInt();
							List<KeyValuePair> list = HotlidTemperatures.Where((KeyValuePair s) => (int)s.Key == (int)keyValue.Key).Take(1).ToList();
							if (list.Count > 0)
							{
								list[0].Value = keyValue.Value;
							}
							else
							{
								HotlidTemperatures.Add(keyValue);
							}
						}
						streamEx.Close();
						break;
					}
					case PropertyKey.Key_CustomImport:
						CustomImport = BytesConverter.BytesToBool(dates[i]);
						break;
					case PropertyKey.Key_CustomImportName:
						CustomImportName = BytesConverter.BytesToString(dates[i]);
						break;
					case PropertyKey.Key_AutoSaveOption:
						AutoSaveOption = (EAutoSaveType)BytesConverter.BytesToInt(dates[i]);
						break;
					case PropertyKey.Key_FileExt:
						FileExt = BytesConverter.BytesToString(dates[i]);
						break;
					}
				}
				return true;
			}
			stream.Seek(-4L, SeekOrigin.Current);
			while (true)
			{
				switch (stream.ReadKey())
				{
				case PropertyKey.Key_WellHighLight:
					stream.ReadBool();
					break;
				case PropertyKey.Key_InfoHighLight:
					stream.ReadBool();
					break;
				case PropertyKey.Key_CurveHighLight:
					stream.ReadBool();
					break;
				case PropertyKey.Key_WellSelectMode:
					stream.ReadInt();
					break;
				case PropertyKey.Key_PressCtlCanSelectWell:
					stream.ReadBool();
					break;
				case PropertyKey.Key_ChannelSelectMode:
					stream.ReadInt();
					break;
				case PropertyKey.Key_UseSubset:
					stream.ReadBool();
					break;
				case PropertyKey.Key_AMPAlgorithm:
					stream.ReadInt();
					break;
				case PropertyKey.Key_OpenHotlidDelayTime:
					stream.ReadInt();
					break;
				case PropertyKey.Key_DefaultHotlidTemperature:
					stream.ReadInt();
					break;
				case PropertyKey.Key_HotlidTemperatureList:
				{
					int num2 = stream.ReadInt();
					for (int k = 0; k < num2; k++)
					{
						KeyValuePair keyValue2 = new KeyValuePair();
						keyValue2.Key = (EProjectType)stream.ReadInt();
						keyValue2.Value = stream.ReadInt();
						List<KeyValuePair> list2 = HotlidTemperatures.Where((KeyValuePair s) => (int)s.Key == (int)keyValue2.Key).Take(1).ToList();
						if (list2.Count > 0)
						{
							list2[0].Value = keyValue2.Value;
						}
						else
						{
							HotlidTemperatures.Add(keyValue2);
						}
					}
					break;
				}
				case PropertyKey.Key_Compensator:
					Compensator = stream.ReadBool();
					break;
				default:
					stream.Seek(-4L, SeekOrigin.Current);
					return true;
				}
			}
		}

		public List<KeyValuePair> GetAllTypeTemperatures()
		{
			if (_HotlidTemperatures == null)
			{
				_HotlidTemperatures = new List<KeyValuePair>();
				for (EProjectType eProjectType = EProjectType.AQ; eProjectType < EProjectType.Last; eProjectType++)
				{
					_HotlidTemperatures.Add(new KeyValuePair
					{
						Key = eProjectType,
						Value = 105
					});
				}
			}
			return _HotlidTemperatures;
		}
	}
}
